package com.ids.idtma.map;

import java.util.ArrayList;

import com.baidu.mapapi.map.offline.MKOLSearchRecord;
import com.baidu.mapapi.search.core.CityInfo;
import com.ids.idtma.IdtApplication;
import com.ids.idtma.R;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NationalCityAdapter extends BaseExpandableListAdapter {
	private final Activity context;
	private LayoutInflater inflater;
	private IdtApplication idtApplication;
	private ArrayList<MKOLSearchRecord> childcities;

	public NationalCityAdapter(Activity context,
			ArrayList<MKOLSearchRecord> childCities) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		idtApplication = (IdtApplication) context.getApplication();
		this.childcities = childCities;
	}

	// 获取省份的城市实体类
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childcities.get(groupPosition).childCities.get(childPosition);
	}

	// 获取省份的城市的listView数量
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	// 进行城市列表的item操作
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		FatherHolder groupHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.map_offline_shengfen, null);
			groupHolder = new FatherHolder();
			// groupHolder.idt_group_fold_arrow_image = (ImageView) convertView
			// .findViewById(R.id.cityButton);
			groupHolder.groupName = (TextView) convertView
					.findViewById(R.id.shengFen_name);
			groupHolder.groupSize = (TextView) convertView
					.findViewById(R.id.shengFen_size);
			convertView.setTag(groupHolder);
		} else {
			groupHolder = (FatherHolder) convertView.getTag();
		}
		groupHolder.groupName
				.setText(childcities.get(groupPosition).childCities
						.get(childPosition).cityName);
		groupHolder.groupSize.setText("--"
				+ formatDataSize(childcities.get(groupPosition).childCities
						.get(childPosition).size));

		return convertView;
	}

	// 得到省份下面的城市数量
	@Override
	public int getChildrenCount(int groupPosition) {
		return childcities.get(groupPosition).childCities == null ? 0
				: childcities.get(groupPosition).childCities.size();
	}

	// 获取到省份的实体类
	@Override
	public Object getGroup(int groupPosition) {
		return childcities.get(groupPosition);
	}

	// 获取省份的大小
	@Override
	public int getGroupCount() {
		return childcities == null ? 0 : childcities.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		ChildHolder childHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.map_offline_city, null);
			childHolder = new ChildHolder();
			childHolder.imageDown = (ImageView) convertView
					.findViewById(R.id.cityButton);
			if (childcities == null) {
				return null;
			}
			childHolder.cityName = (TextView) convertView
					.findViewById(R.id.city_name);

			childHolder.citySize = (TextView) convertView
					.findViewById(R.id.city_size);
			convertView.setTag(childHolder);
		} else {
			childHolder = (ChildHolder) convertView.getTag();
		}
		childHolder.cityName.setText(childcities.get(groupPosition).cityName);
		if (childcities.get(groupPosition).childCities == null) {
			childHolder.citySize.setText("--"+formatDataSize(childcities
					.get(groupPosition).size));
			childHolder.imageDown.setVisibility(View.GONE);
		}
		
		 if(isExpanded){
			 childHolder.imageDown .setBackgroundResource(R.drawable.new_ui_group_page_arrow_open_button);
         }else{
        	 childHolder.imageDown .setBackgroundResource(R.drawable.new_ui_group_page_arrow_close_button);
         }
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	public String formatDataSize(int size) {
		String ret = "";
		if (size < (1024 * 1024)) {
			ret = String.format("%dK", size / 1024);
		} else {
			ret = String.format("%.1fM", size / (1024 * 1024.0));
		}
		return ret;
	}

	// 设置子选项可以选择
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	class FatherHolder {
		// 箭头
		public ImageView idt_group_fold_arrow_image;
		// 省份名称
		public TextView groupName;
		// 省份大小
		public TextView groupSize;

	}

	class ChildHolder {
		// 下载
		public ImageView imageDown;
		// 城市名称
		public TextView cityName;
		// 城市大小
		public TextView citySize;

	}

}
